﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.Plugin;
using iLangl.LanguageWire.DAL.LanguageWireDAL;
using iLangl.Customer.Actions;
using iLangl.Plugin.ConfigFileSettings;

namespace iLangl.LanguageWire.Managers
{
	public class PluginManagerLW : BaseManager
	{
		#region Singelton Pattern
		private PluginManagerLW()
		{
		}

		private static PluginManagerLW instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the SettingsManager.
		/// </summary>
		public static PluginManagerLW Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new PluginManagerLW();
						}
					}
				}
				return instance;
			}
		}
		#endregion

		public String RegisterPluginLicense( )
		{
			//Prepare Request
			PluginRegistration request = new PluginRegistration();

			request.PluginId = "B6C10D99-4E9A-4404-BEF5-B77C9EC7BE64";
			request.PluginName = "iLangl LanguageWire Plugin";
			request.PluginVersion = "1";
			request.SessionId = new PluginAuthenticationService().Authenticate(String.Empty);
			request.PushURIAddress = "http://localhost/languagewireAPI";
			request.Company = DALFacadeLW.Instance.CompanyDAL.GetLWCompanyInfo();

			Contact contact = SettingsManager.Instance.GetDefaultContact();
			request.Company.AddContact(contact);
			request.PrimaryContactId = contact.Id;


			RegisterPluginAction registerPluginAction = new RegisterPluginAction(request);

			registerPluginAction.Send();

			if (String.IsNullOrEmpty(registerPluginAction.PluginLicenceKey))
				throw new ApplicationException("Failed to register plugin. Plugin License Key is Empty!");

			PluginConfiguration.Instance.SetPluginLicenseKey(registerPluginAction.PluginLicenceKey);

			PluginConfiguration.Instance.Save();

			//TODO: Save license key
			return registerPluginAction.PluginLicenceKey;
		}


	}
}
